package com.lh.java.basic.threadlocal;

import java.util.concurrent.TimeUnit;

class MyObject implements Runnable {
    private Monitor monitor;

    public MyObject(Monitor monitor) {
      this.monitor = monitor;
    }

    public void run() {
      try {
    	System.out.println("i'm going.");
        TimeUnit.SECONDS.sleep(3);       
        monitor.gotMessage();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
}

class Monitor implements Runnable {
    private volatile boolean go = false;

    public synchronized void  gotMessage() throws InterruptedException {
      go = true;
      notify();
    }

    public synchronized void  watching() {
      while (go == false){
		try {
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
      }
      System.out.println("He has gone.");
    }

    public void run() {
      watching();
    }
}

public class BusyWaiting {
    public static void main(String[] args) {
      Monitor monitor = new Monitor();
      MyObject o = new MyObject(monitor);
      new Thread(o).start();
      new Thread(monitor).start();
    }
}
